import { Component, OnInit } from '@angular/core';
import { ApiService } from './../../../../core/api/api.service';
import { FormBuilder, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { MessageService } from './../../../../core/service/message.service';
import { Router } from '@angular/router';

@Component({
    selector: 'app-member-center-home',
    templateUrl: './department.html',
    styles: [
        `
          [nz-button] {
            margin-right: 8px;
            margin-bottom: 12px;
          }
        `
      ]
})
export class DepartmentComponent implements OnInit {
    page = {pageNo: 1};
    departmentList = [];
    departmentTypes = {};
    selectedRowData: any = {};
    departmentTypesList = [];
    isVisible = false;
    editTable = false;
    validateForm;
    constructor(
        private apiService: ApiService,
        private fb: FormBuilder,
        private message: NzMessageService,
        private msg: MessageService,
        private router: Router
        ) {
    }
    ngOnInit() {
        this.msg.setTitle('科室维护');
        this.apiService.post('医院科室类别列表').then((res: any) => {
            this.departmentTypesList = res. data.list;
            res.data.list.forEach(element => {
                this.departmentTypes[element.id] = element.name;
            });
            this.loadList();
        });
        this.validateForm = this.fb.group({
            departmentName: [null, [Validators.required]],
            departmentTypeId: [null, [Validators.required]],
            memo: [null],
        });
    }
    loadList() {
        this.apiService.post('医院科室列表', {...this.page}).then((res: any) => {
            this.departmentList = res.data.list;
        });
    }
    updateTable() {
        this.editTable = !this.editTable;
    }
    handleCancel() {
        this.isVisible = false;
    }
    handleOk() {
        for (const key in this.validateForm.controls) {
            this.validateForm.controls[key].markAsDirty();
            this.validateForm.controls[key].updateValueAndValidity();
        }
        if (this.validateForm.valid) {
            this.apiService.post('创建医院科室', {...this.validateForm.value , hospitalName: '', medicalCode: '', permission: [] }).then((res: any) => {
                this.isVisible = false;
                this.validateForm.reset();
                this.message.success('创建科室成功！');
                this.loadList();
            });
        }

    }
    add() {
        this.isVisible = true;
    }
    edit() {
        if (!this.selectedRowData.id) {
            this.message.warning('请选择一条需要修改的数据！');
            return;
        }
        this.router.navigate(['/memberCenter/administrator/departmentUpdate', this.selectedRowData.id]);
    }
    selectedRow(data) {
        this.selectedRowData = data;
    }
}
